import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/a_elder/page/elder_page.dart';
import 'package:ls_work/a_elder/page/elder_search_page.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/net/post_request.dart';

class ActivitySignViewModel extends BaseViewModel {
  final ElderInfo? info;

  ActivitySignViewModel(BuildContext context,{this.info}) : super(context){
    this.older = info;
    notifyListeners();
  }

  ElderInfo? older;
  var selectDate = "";
  var _submitDate = "";
  var activityStr = "";

  Future<void> selectOlder() async {
    older = await Go().push(ElderSearchPage(
      needBack: true,isSelected: false,
    ));
    notifyListeners();
  }

  void selectTime(){
    var format = [yyyy,"-",mm,"-",dd," ",HH,":",nn,":",ss];
    var format2 = [yyyy,"-",mm,"-",dd," ",HH,":",nn];
    DatePicker.showDateTimePicker(context,onConfirm: (date){
      selectDate = formatDate(date!, format2, LocaleType.zh);
      _submitDate = formatDate(date!, format, LocaleType.zh);
      notifyListeners();
    });
  }

  Future<void> submitSign() async {
    if(older==null){
      showToast("请选择长者");
      return;
    }

    if(_submitDate.isEmpty){
      showToast("请选择时间");
      return;
    }

    if(activityStr.isEmpty){
      showToast("请输入参与活动");
      return;
    }

    showLoadingDialog(msg: "提交中...");
    try{
      var map = await PostRequest.signActivity(activityStr, older?.id?.toString()??"", _submitDate);
      hideDialog();
      if("0"==map["code"].toString()){
        showToast("提交成功");
        sendMessage(SendMessageEvent(MessageConfig.FINISH_ACTIVITY_SIGN));
        finish();
      }else{
        showToast(map["msg"]);
      }
    }catch(e){
      hideDialog();
    }
  }
}
